package com.webvihar.common.utils;

/**
 * @author : Viswanath Kumar sandu, Feb 28, 2012
 * 
 * Ref : http://www.mkyong.com/regular-expressions/10-java-regular-expression-examples-you-should-know/
 */

public class Validation {

	// Regular Expressions for all possible data types
	private static final String emailPattern = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
	private static final String usernamePattern = "^[a-z0-9_-]{3,15}$";
	private static final String ipAddressPattern = "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";
	private static final String urlPattern = "^[a-zA-Z0-9\\-\\.]+\\.(com|org|net|mil|edu|COM|ORG|NET|MIL|EDU)$";
	private static final String localHost = "localhost";
	
	
	public static Boolean isEmpty(String data) {
		return (data != null && data.length() == 0);
	}

	public static Boolean isString(String data) {
		return (data != null && !isEmpty(data));
	}
	
	public static Boolean isField(String data) {
		return (data != null && !isString(data));
	}
	
	public static Boolean isType(String data) {
		return (data != null && !isString(data));
	}
	
	public static Boolean isLength(String data) {
		return (data != null && !isString(data));
	}
	
	public static Boolean isDefault(String data) {
		return (data != null);
	}
	
	public static Boolean isCollation(String data) {
		return (data != null);
	}
	
	public static Boolean isComment(String data) {
		return (data != null);
	}

	// Returns true if the string is an email
	public static boolean isEmail(String s){
		return s.matches(emailPattern);
	}
	
	public static Boolean isUsername(String s) {
		return s.matches(usernamePattern);
	}

	public static Boolean isIpAddress(String s) {
		return s.matches(ipAddressPattern);
	}
	
	public static Boolean isUrl(String s) {
		return s.matches(urlPattern);
	}
	
	public static Boolean isLocalHost(String s) {
		return s.matches(localHost);
	}
	
	// return true if the host is an ip or url or localhost
	public static Boolean isHost(String host) {
		return (isIpAddress(host) || isUrl(host) || isLocalHost(host));
	}

	public static Boolean isPort(String port) {
		if(port != null && !isEmpty(port)) {
			int prt = Integer.parseInt(port);
			return isPort(prt);
		}
		return false;
	}

	public static Boolean isPort(int port){
		if(port > 0 && port < 9999) {
			return true;
		} else {
			return false;
		}
	}

	public static Boolean isPassword(String password) {
		return true;
	}
	
	public static boolean isDbName(String dbname) {
		return (dbname != null);
	}
}
